package com.itheima.health.service.impl;

import com.itheima.health.DTO.OrderDTO;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderService;
import com.itheima.health.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private MemberDao memberDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private OrderSettingDao ordersettingDao;
    @Resource
    private SetmealDao setmealDao;

    @Transactional
    @Override
    public Order submit(OrderDTO orderDTO) {
        Integer validateCode = (Integer) redisTemplate.opsForValue().get(orderDTO.getTelephone() + "ORDER");
        //判断验证码是否正确
        if (validateCode != null && orderDTO.getValidateCode().equals(validateCode.toString())) {
            Member member = memberDao.selectByIdCard(orderDTO.getIdCard());
            //判断会员是否注册
            if (member == null) {
                member = new Member();
                member.setName(orderDTO.getName());
                member.setSex(String.valueOf(orderDTO.getSex()));
                member.setIdCard(orderDTO.getIdCard());
                member.setPhoneNumber(orderDTO.getTelephone());
                member.setRegTime(new Date());

                memberDao.save(member);
            }
            Order order = orderDao.selectByDateAndSetmealIdAndMemberId(orderDTO.getOrderDate(), orderDTO.getSetmealId(), member.getId());
            //判断会员同⼀天同⼀套餐是否重复预约
            if (order == null) {
                OrderSetting orderSetting = ordersettingDao.selectByOrderDate(orderDTO.getOrderDate());

                //判断体检预约是否已经约满
                if (orderSetting.getNumber() != 0) {
                    Order orderdb = new Order();
                    orderdb.setMemberId(member.getId());
                    orderdb.setOrderDate(orderDTO.getOrderDate());
                    orderdb.setOrderType(Order.ORDERTYPE_WEIXIN);
                    orderdb.setOrderStatus(Order.ORDERSTATUS_NO);
                    orderdb.setSetmealId(orderDTO.getSetmealId());

                    orderDao.save(orderdb);

                    //更新后台预约数量
                    orderSetting.setNumber(orderSetting.getNumber() - 1);
                    orderSetting.setReservations(orderSetting.getReservations() + 1);
                    ordersettingDao.update(orderSetting);
                    log.info(MessageConst.ORDER_SUCCESS);
                    return orderdb;
                } else {
                    throw new BusinessRuntimeException(MessageConst.ORDER_FULL);
                }
            } else
                throw new BusinessRuntimeException(MessageConst.HAS_ORDERED);
        } else
            throw new BusinessRuntimeException(MessageConst.VALIDATECODE_ERROR);
    }

    @Override
    public OrderVO findById(Integer id) {
        Order order = orderDao.selectById(id);
        OrderVO orderVO = new OrderVO();
        //Date类型转String类型
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = simpleDateFormat.format(order.getOrderDate());
        orderVO.setOrderDate(dateStr);
        orderVO.setOrderType(order.getOrderType());
        Member member = memberDao.selectById(order.getMemberId());
        orderVO.setMember(member.getName());
        Setmeal setmeal = setmealDao.selectbyId(order.getSetmealId());
        orderVO.setSetmeal(setmeal.getName());

        return orderVO;
    }
}
